﻿using AgencyManagerSystem.AsmType;
using AgencyManagerSystem.Common;
using AgencyManagerSystem.Core;
using AgencyManagerSystem.DataFetcher;
using AgencyManagerSystem.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;

namespace AgencyManagerSystem.Controllers
{
    public class RoleController : MyControllerBase
    {
        RoleManager manager;
        RoleFetcher fetcher;
        RoleTableFetcher dFetcher;

        public RoleController() : base()
        {
            this.manager = new RoleManager();
            this.fetcher = new RoleFetcher();
            this.dFetcher = new RoleTableFetcher();
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute( PolicyDomain.Super, AppOpt.Create)]
        public AsmMainDetail<RoleModel,RoleTableModel> Set(dynamic roleModelJson)
        {
            string mainStr = (String)roleModelJson.main;
            string detailStr = (String)roleModelJson.detail;
            RoleModel main = Util.ConvertObjectFromJson<RoleModel>(mainStr) ;
            IEnumerable<RoleTableModel> details = Util.ConvertArrayFromJson<RoleTableModel>(detailStr);
            LogHelper.Debug(this.GetType(), String.Format("SET:[RQS|{2}]{0}|[DETAIL]{1}",mainStr, detailStr, RequestIp));
            if (main == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("SET:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                var errResponse = new AsmMainDetail<RoleModel, RoleTableModel>() { Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
                LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            foreach (var item in details)
            {
                item.Role = main.AutoId;
            }
            var result = manager.Set(main,details);
            LogHelper.Debug(this.GetType(), String.Format("SET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            DBHelper.Commit( result.Result);
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Super, AppOpt.Delete)]
        public AsmObject<bool> Del(RoleModel roleModel)
        {
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RQS|{1}]{0}", Util.ConvertToJson(roleModel), RequestIp));
            if (roleModel == null)
            {
                DBHelper.Commit( false);
                LogHelper.Err(this.GetType(), String.Format("DEL:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (int)HttpStatusCode.BadRequest;
                return new AsmObject<bool>() { Data = false, Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
            }
            AsmObject<bool> result = manager.Delete(roleModel);
            DBHelper.Commit( result.Result);
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpPost]
        [RoleFlagAuthorizeAttribute(PolicyDomain.Super, AppOpt.Delete)]
        public AsmArray<Int32> BulkDel(dynamic jsonUserModel)
        {
            LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RQS|{1}]{0}", jsonUserModel, RequestIp));
            var models = Util.ConvertArrayFromJson<RoleModel>((String)jsonUserModel.data);
            if (models == null)
            {
                LogHelper.Err(this.GetType(), String.Format("BULKDEL:从{0}中接收到空的参数", RequestIp));
                Response.StatusCode = (Int32)HttpStatusCode.BadRequest;
                var errResponse = new AsmArray<Int32>() { Data = new List<Int32>(), Message = "传入的参数不能为空", Result = AmsCode.ArgNotAccept };
                LogHelper.Debug(this.GetType(), String.Format("BULKDEL:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            int faild = models.Count();
            var del = new List<Int32>();
            StringBuilder sb = new StringBuilder();
            foreach (var model in models)
            {
                var eff = manager.Delete(model);
                if (eff.Result != 0)
                {
                    sb.AppendFormat("{0}:{1}\r\n", model.AutoId, eff.Message);
                }
                else
                {
                    del.Add(model.AutoId);
                    faild--;
                }
            }
            DBHelper.Commit(true);
            var succResponse = new AsmArray<Int32>() { Data = del, Message = "共有" + (models.Count() - faild) + "笔资料删除成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("DEL:[RSP|{1}]{0}", Util.ConvertToJson(succResponse), RequestIp));
            return succResponse;
        }
        [HttpGet]
        [Authorize]
        public AsmArray<RoleModel> List(string jsonCondition, int pos, int size)
        {
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RQS|{1}]{0}", jsonCondition, RequestIp));
            int cnt = 0;
            IList<RoleModel> role = fetcher.List(Util.ConvertArrayFromJson<Condition>(jsonCondition), pos, size, out cnt);
            DBHelper.Commit( true);
            var result = new AsmArray<RoleModel>() { Data = role, Message = "操作成功", Result = AmsCode.Succ, Cnt = cnt };
            LogHelper.Debug(this.GetType(), String.Format("LIST:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmMainDetail<RoleModel,RoleTableModel> Get(int id)
        {
            LogHelper.Debug(this.GetType(), String.Format("GET:[RQS|{1}]{0}", id, RequestIp));
            RoleModel role = fetcher.Get(id);
            if (role == null)
            {
                var errResponse = new AsmMainDetail<RoleModel, RoleTableModel> { Data = role, Detail = null, Message = "找不到数据", Result = AmsCode.DataNotFound };
                DBHelper.Commit(false);
                LogHelper.Debug(this.GetType(), String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(errResponse), RequestIp));
                return errResponse;
            }
            IList<RoleTableModel> roles = dFetcher.ListDetail(id);
            DBHelper.Commit( true);
            var result = new AsmMainDetail<RoleModel, RoleTableModel> { Data = role, Detail = roles, Message = "操作成功", Result = AmsCode.Succ };
            if(role==null)
            {
                result.Message = "找不到数据";
                result.Result = AmsCode.DataNotFound;
            }
            LogHelper.Debug(this.GetType(), String.Format("GET:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
        [HttpGet]
        [Authorize]
        public AsmArray<RoleTableModel> ListDetail(int id)
        {
            LogHelper.Debug(this.GetType(), String.Format("LISTDETAIL:[RQS|{1}]{0}", id, RequestIp));
            var detail = dFetcher.ListDetail(id);
            DBHelper.Commit(true);
            var result = new AsmArray<RoleTableModel> { Data = detail, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("LISTDETAIL:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }

        [HttpGet]
        [Authorize]
        public AsmArray<RoleTableModel> ListByUser(int uid, int domain)
        {
            LogHelper.Debug(this.GetType(), String.Format("LISTBYUSER:[RQS|{1}]{0}", uid, domain, RequestIp));
            var detail = dFetcher.ListByUser(uid,domain);
            DBHelper.Commit(true);
            var result = new AsmArray<RoleTableModel> { Data = detail, Message = "操作成功", Result = AmsCode.Succ };
            LogHelper.Debug(this.GetType(), String.Format("LISTBYUSER:[RSP|{1}]{0}", Util.ConvertToJson(result), RequestIp));
            return result;
        }
    }
}